// import request from '@/utils/request'
import fetch, { request } from '@/utils/request-fetch'
import { requestXhr } from '@/utils/request-xhr'
const VUE_APP_CHAT_API = process.env.VUE_APP_CHAT_API
const VUE_APP_BASE_API = process.env.VUE_APP_BASE_API
/**
 * 获取模型列表
 * http://showdoc.wisetimes.cn:4999/web/#/265/2224
 * @param {*} data
 * @returns
 */
export function getModels (params) {
  return request('GET', `${VUE_APP_CHAT_API}/api/v2/wechatgpt/conversation/models`, params)
}
/**
 * 对话
 * http://showdoc.wisetimes.cn:4999/web/#/265/2225
 * @param {*} data
 * @returns
 */
export function conversation (data, options) {
  const url = data.knowledge_id
    ? '/api/v1/wechatgpt/knowledge/chat'
    : '/api/v2/wechatgpt/conversation'
  return fetch('POST', `${VUE_APP_CHAT_API}${url}`, data, options)
}
// 权益相关
export function getRightsByCat (data) {
  return request(
    'POST',
    VUE_APP_BASE_API + `/api/v2/right_package/by_pid`,
    data,
    {
      isEncrypt: true // 加密
    }
  )
}

/**
 * 获取对话历史
 * @param {*} data
 * @returns
 */
export function getHistory (opt) {
  const params = {
    page_size: opt.page_size || 15,
    ...opt
  }
  return request(
    'GET',
    `${VUE_APP_CHAT_API}/api/v2/wechatgpt/conversation/list`,
    params
  )
}
/**
 * 删除历史
 * @param {*} data
 * @returns
 */
export function clearAllConversation () {
  return request('POST', `${VUE_APP_CHAT_API}/api/v2/wechatgpt/conversation/clear`)
}
export function deleteConversation (id) {
  return request('POST', `${VUE_APP_CHAT_API}/api/v2/wechatgpt/conversation/${id}/delete`)
}
export function starConversation (id) {
  return request('POST', `${VUE_APP_CHAT_API}/api/v2/wechatgpt/conversation/${id}/star`)
}
export function unStarConversation (id) {
  return request('POST', `${VUE_APP_CHAT_API}/api/v2/wechatgpt/conversation/${id}/unstar`)
}
/**
 * 对话update
 * @param {*} data
 * @returns
 */
export function updateConversation (id, title) {
  return request('POST', `${VUE_APP_CHAT_API}/api/v2/wechatgpt/conversation/${id}/update`, { title })
}

/**
 * 对话stop
 * @param {*} data
 * @returns
 */
export function conversationStop (conversionId, messageId) {
  return request('POST', `${VUE_APP_CHAT_API}/api/v2/wechatgpt/conversation/${conversionId}/stop`, {
    message_id: messageId
  })
}

export function getConversationMessages (id) {
  return request('GET', `${VUE_APP_CHAT_API}/api/v2/wechatgpt/conversation/${id}/messages`)
}

export function getDocumentSummary (knowledge_id, document_id) {
  return request(
    'POST',
    `${VUE_APP_CHAT_API}/api/v1/wechatgpt/knowledge/${knowledge_id}/document/${document_id}/summary`
  )
}

export function getDocumentSuggestion (knowledge_id) {
  return request(
    'GET',
    `${VUE_APP_CHAT_API}/api/v1/wechatgpt/knowledge/${knowledge_id}/suggested_questions`
  )
}

export function likeMessage (id, messageId, status) {
  return request(
    'POST',
    `${VUE_APP_CHAT_API}/api/v2/wechatgpt/conversation/${id}/message/${messageId}/like`,
    { like_status: status }
  )
}

export function getKnowledgeStatusById (document_id) {
  return request(
    'GET',
    `${VUE_APP_CHAT_API}/api/v1/wechatgpt/knowledge/${document_id}/status`
  )
}

// 上传文档并自动创建知识库
export async function uploadDocumentByUrl (url, title, content, model) {
  const data = new FormData()
  data.append('is_add_conversation', 'true')
  data.append('html_data_url', url)
  data.append('html_data', content)
  data.append('title', title)
  data.append('knowledge_type', model)

  return request(
    'POST',
    `${VUE_APP_CHAT_API}/api/v1/wechatgpt/knowledge/document/upload`,
    data,
    { noSetType: true }
  )
}

export function uploadDocumentByFile (file, knowledge_type, name, process) {
  const data = new FormData()
  data.append('is_add_conversation', 'true')
  data.append('file', file)
  data.append('knowledge_type', knowledge_type)
  data.append('title', name)
  data.append('skip_auto_summary', 'true')
  return requestXhr(
    'POST',
    `${VUE_APP_CHAT_API}/api/v1/wechatgpt/knowledge/document/upload`,
    data,
    { noSetType: true, process }
  )
}

/**
 * 获取项目功能列表
 * http://192.168.88.247:4999/web/#/283/2495 密码: RR8pUR67
 * @returns
 */
export function getFnPrice (params = {}) {
  return request('GET', VUE_APP_BASE_API + '/charge/api/v1/fn_list', params, { authBear: true })
}
